//
//  TimeView.m
//  time
//
//  Created by XXX on 2017/5/3.
//  Copyright © 2017年 lee. All rights reserved.
//

#import "TimeView.h"

//static CGFloat TV_CircleWidth = 150;//表的直径

@implementation TimeView{
    CGFloat TV_CircleWidth;//表的直径
    CGPoint o;//圆心坐标
    
    UIView *panelView;//钟表面板
    UIView *secondsView;//秒针
    UIView *minutesView;//分针
    UIView *hoursView;//时针
    UIView *centerView;//表心
    UILabel *timePeriodLabel;//时间段
    
    /**各控件颜色*/
    UIColor *oneColor;//表框及时针
    UIColor *twoColor;//表盘
    UIColor *threeColor;//分针及刻度
    UIColor *fourColor;//秒针及表芯
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor whiteColor];

        [self aboutColors:0];
        [self initView:frame];
        [self aboutChangeTime];
    }
    return self;
    
}

-(void)aboutColors:(int)flag{
    
    switch (flag) {
        case 0:
        {
            /**简约清新*/
            oneColor = [UIColor redColor];//表框及时针
            twoColor = [UIColor whiteColor];//表盘
            threeColor = [UIColor blackColor];//分针及刻度
            fourColor = [UIColor orangeColor];//秒针及表芯
        }
            break;
        case 1:
        {
            /**黝黑夜光*/
            oneColor = [UIColor greenColor];//表框及时针
            twoColor = [UIColor blackColor];//表盘
            threeColor = [UIColor orangeColor];//分针及刻度
            fourColor = [UIColor whiteColor];//秒针及表芯
        }
            break;
        case 2:
        {
            /**典雅商务*/
            oneColor = [UIColor grayColor];
            twoColor = [UIColor blackColor];
            threeColor = [UIColor whiteColor];
            fourColor = [UIColor grayColor];

        }
            break;

            
        default:
            break;
    }
   
    
   }

- (void)initView:(CGRect)frame{
    
    TV_CircleWidth = frame.size.width;
    CGFloat r = TV_CircleWidth/2;//半径
    o = CGPointMake(frame.size.width/2, frame.size.height/2);//圆心坐标
    
    
    panelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TV_CircleWidth, TV_CircleWidth)];
    panelView.layer.borderWidth = 2;
    panelView.layer.cornerRadius = r;
    panelView.layer.masksToBounds = YES;
    [self addSubview:panelView];

    //秒针
    secondsView = [[UIView alloc]initWithFrame:CGRectMake(o.x-1, o.y-r+5, 2, r-5)];
    secondsView.layer.cornerRadius = 1;
    [self addSubview:secondsView];
    
    //分针
    minutesView = [[UIView alloc]initWithFrame:CGRectMake(o.x-2, o.y-r+20, 4, r-20)];
    minutesView.layer.cornerRadius = 2.0;
    [self addSubview:minutesView];

    //时针
    hoursView = [[UIView alloc]initWithFrame:CGRectMake(o.x-2.5, o.y-r+30, 5, r-30)];
    hoursView.layer.cornerRadius = 2.5;
    [self addSubview:hoursView];
    
    //表心
    centerView = [[UIView alloc]initWithFrame:CGRectMake(o.x-7.5, o.y-7.5, 15, 15)];
    centerView.layer.cornerRadius = 7.5f;
    [self addSubview:centerView];
    
    //时间段
    timePeriodLabel = [[UILabel alloc]initWithFrame:CGRectMake((panelView.frame.size.width-35)/2, panelView.frame.origin.y+35, 35, 15)];
    timePeriodLabel.backgroundColor = [UIColor clearColor];
    timePeriodLabel.font = [UIFont fontWithName:@"Times New Roman" size:13];
    timePeriodLabel.text = @"●●";
    timePeriodLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:timePeriodLabel];

    //刻度
    NSArray *textArray = @[@"12",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11"];
    for (int i = 0; i<textArray.count; i++) {
        NSString *str = textArray[i];
        //坐标
        CGFloat x =o.x + sin(2*M_PI/12*i)*(r-15);
        CGFloat y =o.y - cos(2*M_PI/12*i)*(r-15);
        NSLog(@"%f,%f",x,y);
        
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(x-10, y-10, 20, 20)];
        lab.tag = 10+i;
        lab.font = [UIFont boldSystemFontOfSize:15];
        lab.textAlignment = NSTextAlignmentCenter;
        lab.text = str;
        [self addSubview:lab];
    }
    
    [self setColors];
    
}

//设置颜色
-(void)setColors{
    panelView.backgroundColor = twoColor;
    panelView.layer.borderColor = oneColor.CGColor;
    secondsView.backgroundColor = fourColor;
    minutesView.backgroundColor = threeColor;
    hoursView.backgroundColor = oneColor;
    centerView.backgroundColor = fourColor;
    timePeriodLabel.textColor = threeColor;
    for (int i = 0; i<12; i++) {
        UILabel *lab = (UILabel *)[self viewWithTag:10+i];
        lab.textColor = threeColor;
    }
}

-(void)aboutChangeTime{
    [self startGCDTimer];
}
-(void) startGCDTimer{
    
    // GCD定时器
    static dispatch_source_t _timer;
    NSTimeInterval period = 1.0; //设置时间间隔
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行
    // 事件回调
    dispatch_source_set_event_handler(_timer, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSDate *currentDate = [NSDate date];//获取当前时间，日期
            NSDateFormatter *dateFormatterSS = [[NSDateFormatter alloc] init];
            NSDateFormatter *dateFormatterMM = [[NSDateFormatter alloc] init];
            NSDateFormatter *dateFormatterHH = [[NSDateFormatter alloc] init];
            [dateFormatterSS setDateFormat:@"ss"];
            [dateFormatterMM setDateFormat:@"mm"];
            [dateFormatterHH setDateFormat:@"HH"];
            NSInteger ss = [[dateFormatterSS stringFromDate:currentDate] integerValue];
            CGFloat mm = [[dateFormatterMM stringFromDate:currentDate] floatValue];
            NSInteger hh = [[dateFormatterHH stringFromDate:currentDate] integerValue];
            NSLog(@"%ld,%f,%ld",hh,mm,ss);
            if(hh>=12){
                timePeriodLabel.text = @"PM";
                if(hh>12){
                    hh = hh -12;
                }
            }else{
                timePeriodLabel.text = @"AM";
            }

            secondsView.transform = CGAffineTransformIdentity;
            secondsView.transform = GetCGAffineTransformRotateAroundPoint(secondsView.center.x,secondsView.center.y,o.x,o.y,(2*M_PI/60 * ss));
            
            minutesView.transform = CGAffineTransformIdentity;
            minutesView.transform = GetCGAffineTransformRotateAroundPoint(minutesView.center.x,minutesView.center.y,o.x,o.y,(2*M_PI/60 * mm));
            
            hoursView.transform = CGAffineTransformIdentity;
            hoursView.transform = GetCGAffineTransformRotateAroundPoint(hoursView.center.x,hoursView.center.y,o.x,o.y,(2*M_PI/12 * (hh+mm/60)));
            
        });
    });
    
    // 开启定时器
    dispatch_resume(_timer);
    
    // 关闭定时器
    // dispatch_source_cancel(_timer);
}

CGAffineTransform  GetCGAffineTransformRotateAroundPoint(float centerX, float centerY, float x ,float y ,float angle)
{
    x = x - centerX; //计算(x,y)从(0,0)为原点的坐标系变换到(CenterX ，CenterY)为原点的坐标系下的坐标
    y = y - centerY; //(0，0)坐标系的右横轴、下竖轴是正轴,(CenterX,CenterY)坐标系的正轴也一样
    
    CGAffineTransform  trans = CGAffineTransformMakeTranslation(x, y);
    trans = CGAffineTransformRotate(trans,angle);
    trans = CGAffineTransformTranslate(trans,-x, -y);
    return trans;
}

-(void)changeStyle:(int)flag{
    NSLog(@"更换主题");
    [self aboutColors:flag];
    [self setColors];
    
}

@end
